from asyncmoex.base import AsyncMoexBase, Interval


class AsyncMoex(AsyncMoexBase):
    async def get_securities(
            self,
            search: str | None = None,
            is_trading: bool | None = None,
    ):
        """
        Получить список бумаг торгуемых на московской бирже.

        :param search:
        :param is_trading:
        :return:
        """

        query_params = {}
        if search:
            query_params["q"] = search

        if is_trading is not None:
            query_params["is_trading"] = 1 if is_trading else 0

        response = await self._make_multi_requests(
            "securities",
            "securities",
            query_params,
        )

        data = self._parse_list(response["securities"])
        return data

    async def get_security(self, security: str) -> tuple[list[dict], list[dict]]:
        response = await self._make_request(f"securities/{security}")
        description = self._parse_list(response["description"])
        boards = self._parse_list(response["boards"])
        return description, boards

    async def get_engines(self) -> list[dict]:
        response = await self._make_request("engines")
        data = self._parse_list(response["engines"])
        return data

    async def get_engine(self, engine: str) -> tuple[list[dict], list[dict], list[dict]]:
        response = await self._make_request(f"engines/{engine}")
        engine = self._parse_list(response["engine"])
        timetable = self._parse_list(response["timetable"])
        dailytable = self._parse_list(response["dailytable"])
        return engine, timetable, dailytable

    async def get_engine_markets(self, engine: str):
        response = await self._make_request(f"engines/{engine}/markets")
        data = self._parse_list(response["markets"])
        return data

    async def get_market_securities(self, engine: str, market: str):
        response = await self._make_request(f"engines/{engine}/markets/{market}/securities")
        data = self._parse_list(response["securities"])
        return data

    async def get_market_boards(self, engine: str, market: str):
        response = await self._make_request(f"engines/{engine}/markets/{market}/boards")
        data = self._parse_list(response["boards"])
        return data

    async def get_board_securities(self, engine: str, market: str, board: str):
        response = await self._make_request(f"engines/{engine}/markets/{market}/boards/{board}/securities")
        data = self._parse_list(response["securities"])
        return data

    async def get_security_candle_dates(self, engine: str, market: str, board: str, security: str):
        response = await self._make_request(f"history/engines/{engine}/markets/{market}/boards/{board}/securities/{security}/dates")
        data = self._parse_list(response["dates"])
        return data

    async def get_security_candles(self, engine: str, market: str, board: str, security: str, interval: Interval = Interval.D1):
        query_params = {
            "interval": interval.value,
        }
        response = await self._make_multi_requests(
            "candles",
            f"engines/{engine}/markets/{market}/boards/{board}/securities/{security}/candles",
            query_params,
        )
        data = self._parse_list(response["candles"])
        return data


